package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentCompanyAlreadyPublishBinding;
import com.sgb.kjwl.model.entity.enterprise.PublishCompanyDetailEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.sgb.kjwl.view.ui.activity.enterprise.MakeOverPublishActivity;
import com.sgb.kjwl.viewmodel.enterprise.PublishViewModel;

public class CompanyAlreadyPublishFragment extends AppFragment {

    FragmentCompanyAlreadyPublishBinding mBinding;
    PublishViewModel mViewModel;
    PublishCompanyDetailEntity mEntity;

    public static CompanyAlreadyPublishFragment newInstance(long id) {
        CompanyAlreadyPublishFragment tradePublishFragment = new CompanyAlreadyPublishFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("id", id);
        tradePublishFragment.setArguments(bundle);
        return tradePublishFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_company_already_publish, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mBinding.setLifecycleOwner(this);
        mViewModel = new ViewModelProvider(this).get(PublishViewModel.class);
        long id = getArguments().getLong("id");
        mBinding.setViewModel(mViewModel);
        mBinding.setContext(getContext());
        mBinding.tvEnable.setOnClickListener(this);
        mBinding.tvOperateDown.setOnClickListener(this);
        mBinding.tvDelete.setOnClickListener(this);

        mViewModel.getCompanyPublishDetail(getContext(), id + "", 4);
        mViewModel.mJump.observe(this.getViewLifecycleOwner(),integer -> {
            if(integer==4){
                ActivityJump.jumpToActivityForResult(getActivity(), MakeOverPublishActivity.class,1, Pair.create("type", integer), Pair.create("entity", mEntity));
            }
        });

        mViewModel.mDeleteSecess_Type.observe(getViewLifecycleOwner(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                getActivity().finish();;
            }
        });

        mViewModel.mPublishCompanyDetailLiveData.observe(getViewLifecycleOwner(), new Observer<PublishCompanyDetailEntity>() {
            @Override
            public void onChanged(PublishCompanyDetailEntity publishCompanyDetailEntity) {
                mBinding.idRootLayout.setVisibility(View.VISIBLE);
                mBinding.setC(publishCompanyDetailEntity);
                mEntity = publishCompanyDetailEntity;
                if (mEntity.getOperatingScope() == null || mEntity.getOperatingScope().equals("")) {
                    mBinding.etFragmentAlreadyPublishScope.setText("/");
                } else {
                    mBinding.etFragmentAlreadyPublishScope.setText(mEntity.getOperatingScope());
                }

                if (mEntity.getOtherInfo() == null || mEntity.getOtherInfo().equals("")) {
                    mBinding.tvFragmentCompanyPublishInfo.setText("/");
                } else {
                    mBinding.tvFragmentCompanyPublishInfo.setText(mEntity.getOtherInfo());
                }
            }
        });
    }


    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_enable://上架
                if (mEntity != null) {
                    DialogHelper.showDialog(getActivity(), "温馨提示", "是否确认上架？", "确定", new DialogFunction() {
                        @Override
                        public void normalRoundDialog() {
                            mViewModel.changeStateMakeoverPublish(getContext(), mEntity.getId(), 4, "1");
                        }
                    });
                }
                break;
            case R.id.tv_operate_down://下架
                if (mEntity != null) {
                    DialogHelper.showDialog(getActivity(), "温馨提示", "是否确认下架？", "确定", new DialogFunction() {
                        @Override
                        public void normalRoundDialog() {
                            mViewModel.changeStateMakeoverPublish(getContext(), mEntity.getId(), 4, "0");
                        }
                    });
                }
                break;
            case R.id.tv_delete://删除
                DialogHelper.showDialog(getActivity(), "温馨提示", "是否确认删除？", "确定", new DialogFunction() {
                    @Override
                    public void normalRoundDialog() {
                        mViewModel.deleteMakeoverPublish(getContext(), mEntity.getId() + "", 4);
                    }
                });
                break;
        }
    }


}
